package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.YonbipTokenUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.OrgAccbook;
import com.ruoyi.system.domain.OrgCqJk;
import com.ruoyi.system.domain.OrgOrgs;
import com.ruoyi.system.service.IOrgAccbookService;
import com.ruoyi.system.service.IOrgCqJkService;
import com.ruoyi.system.service.IOrgOrgsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrgDept;
import com.ruoyi.system.service.IOrgDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * yonbip部门Controller
 * 
 * @author ruoyi
 * @date 2025-09-05
 */
@Controller
@RequestMapping("/system/orgDept")
public class OrgDeptController extends BaseController
{
    private String prefix = "system/orgDept";

    @Autowired

    private IOrgDeptService orgDeptService = SpringUtils.getBean(IOrgDeptService.class);
    private IOrgOrgsService orgOrgsService = SpringUtils.getBean(IOrgOrgsService.class);
    private IOrgAccbookService orgAccbookService=SpringUtils.getBean(IOrgAccbookService.class);
    private IOrgCqJkService orgCqJkService=SpringUtils.getBean(IOrgCqJkService.class);
    @RequiresPermissions("system:orgDept:view")
    @GetMapping()
    public String orgDept()
    {
        return prefix + "/orgDept";
    }

    /**
     * 查询yonbip部门列表
     */
    @RequiresPermissions("system:orgDept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrgDept orgDept)
    {
        ApiQueryDept();
        startPage();
        List<OrgDept> list = orgDeptService.selectOrgDeptList(orgDept);
        return getDataTable(list);
    }

    /**
     * 导出yonbip部门列表
     */
    @RequiresPermissions("system:orgDept:export")
    @Log(title = "yonbip部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrgDept orgDept)
    {
        List<OrgDept> list = orgDeptService.selectOrgDeptList(orgDept);
        ExcelUtil<OrgDept> util = new ExcelUtil<OrgDept>(OrgDept.class);
        return util.exportExcel(list, "yonbip部门数据");
    }

    /**
     * 新增yonbip部门
     */
    @RequiresPermissions("system:orgDept:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存yonbip部门
     */
    @RequiresPermissions("system:orgDept:add")
    @Log(title = "yonbip部门", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrgDept orgDept)
    {
        return toAjax(orgDeptService.insertOrgDept(orgDept));
    }

    /**
     * 修改yonbip部门
     */
    @RequiresPermissions("system:orgDept:edit")
    @GetMapping("/edit/{pkDept}")
    public String edit(@PathVariable("pkDept") String pkDept, ModelMap mmap)
    {
        OrgDept orgDept = orgDeptService.selectOrgDeptByPkDept(pkDept);
        mmap.put("orgDept", orgDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存yonbip部门
     */
    @RequiresPermissions("system:orgDept:edit")
    @Log(title = "yonbip部门", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrgDept orgDept)
    {
        return toAjax(orgDeptService.updateOrgDept(orgDept));
    }

    /**
     * 删除yonbip部门
     */
    @RequiresPermissions("system:orgDept:remove")
    @Log(title = "yonbip部门", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orgDeptService.deleteOrgDeptByPkDepts(ids));
    }
    public void ApiQueryDept(){


        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
                if(StringUtils.isNull(orgCqJk)){
                    continue;
                }
                if(orgCqJk.getCode().equals("0001-GT01")){
                    orgCqJk.setJcode("010701");
                }else if(orgCqJk.getCode().equals("0011-GT01")){
                    orgCqJk.setJcode("010708");
                }
                List<String> lines=new ArrayList<>();
                List<String> linesSb=new ArrayList<>();
                //成功数
                int cnum=0;
                //失败数
                int snum=0;
                OrgDept orgDeptquery=new OrgDept();
                orgDeptquery.setPkOrg(orgAccbook.getPkRelorg());
                List<OrgDept> orgDeptListQuery= orgDeptService.selectOrgDeptList(orgDeptquery);
                    for(OrgDept orgDept: orgDeptListQuery){
                        String parentCode="";
                        if(!"~".equals(orgDept.getPkFatherorg())){
                            parentCode=orgDeptService.selectOrgDeptByPkDept(orgDept.getPkFatherorg()).getCode();
                        }else{
                            parentCode=orgAccbook.getCODE();
                        }

                        lines.add(orgDept.getCode()+"|"+orgDept.getName()+"|"+orgCqJk.getJcode()+"|"+parentCode+"|"+orgDept.getTel()+"|"+orgDept.getEnablestate()) ;
                      cnum++;
                    }
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        "20251104"+"-"+orgCqJk.getJcode()+"-dept.txt",
                        lines,
                        false
                );
                linesSb.add("部门数据导入成功"+cnum+"条");
                linesSb.add("部门数据导入失败"+snum+"条");
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        "20251104"+"-"+orgCqJk.getJcode()+"-dept.txt.ok",
                        linesSb,
                        false
                );
            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
